package com.zlc.atonce.huarongdao.gui;

import com.zlc.atonce.Util.Point;
import com.zlc.atonce.huarongdao.algorithme.HRDImpl;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

class MyGrid extends GridPane {
    double row;
    double col;
    private MyLabel[][] labels;

    public MyGrid() {
        super();
        BorderStroke borderStroke
                = new BorderStroke(Color.BLACK, Color.BLACK, Color.BLACK, Color.BLACK,
                                   BorderStrokeStyle.SOLID, BorderStrokeStyle.SOLID, BorderStrokeStyle.SOLID, BorderStrokeStyle.SOLID,
                                   null,
                                   BorderWidths.DEFAULT,
                                   new Insets(10));
        setBorder(new Border(borderStroke));
        setPrefHeight(520);
        setPrefWidth(415);
        initGrid(5, 4);
        setOnMouseClicked(new OnMouse());
    }

    void initGrid(int row, int col) {
        getChildren().clear();
        this.row = row;
        this.col = col;
        labels = new MyLabel[row][col];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                MyLabel label = new MyLabel(i, j);
                labels[i][j] = label;
                label.prefHeightProperty().bind(heightProperty().divide(row));
                label.prefWidthProperty().bind(widthProperty().divide(col));
                GridPane.setMargin(label, new Insets(MyLabel.MARGIN_VALUE));


                add(label, j, i);
            }
        }
    }

    public double getColSize() {
        return this.getWidth() / col;
    }

    public double getRowSize() {
        return this.getHeight() / row;
    }

    public MyLabel[] getSelectedLabel() {
        return Arrays.stream(labels)
                .flatMap(Arrays::stream)
                .filter(MyLabel::isSelected)
                .filter(l -> !l.isCombined())
                .toArray(MyLabel[]::new);
    }

    public String getIdentity() {
        StringBuilder sb = new StringBuilder();
        for (MyLabel[] label : labels) {
            for (MyLabel myLabel : label) {
                sb.append(myLabel.getCode());
            }
            sb.append(',');
        }
        return sb.substring(0, sb.length() - 1);
    }

    public void updateByIdentity(String id) {
        HRDImpl hrd = HRDImpl.fromIdentity(id);
        initGrid(hrd.rows,hrd.cols);
        Map<Point, List<Point>> ownerRegions = hrd.getOwnerRegions();
        for (Map.Entry<Point, List<Point>> entry : ownerRegions.entrySet()) {
            MyLabel.combine(entry.getValue().stream().map(p -> labels[p.getX()][p.getY()]).toArray(MyLabel[]::new));
        }

    }

    public Set<String> computeNextStep(){
        String identity = getIdentity();
        HRDImpl hrd = HRDImpl.fromIdentity(identity);
        Set<String> nextStep = hrd.getNextStep();
        return nextStep;
    }
    class OnMouse implements EventHandler<MouseEvent> {

        @Override
        public void handle(MouseEvent mouseEvent) {
            MyGrid.this.requestFocus();
            double x = mouseEvent.getX();
            double y = mouseEvent.getY();
//            System.out.printf("鼠标点击： %s,%s .", x, y);
            int colNum = (int) (x / getColSize());
            int rowNum = (int) (y / getRowSize());
//            System.out.printf("网格位置：第 %s 行, 第 %s 列\n", rowNum + 1, colNum + 1);
            labels[rowNum][colNum].setSelected(!labels[rowNum][colNum].isSelected());
        }
    }
}
